package com.adee.gulimall.ware.service.impl;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.common.utils.Query;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.ware.dao.WareSkuDao;
import com.adee.gulimall.ware.entity.WareSkuEntity;
import com.adee.gulimall.ware.feign.ProductFeignService;
import com.adee.gulimall.ware.service.WareSkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Slf4j
@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        try {
            Long skuId = Long.parseLong((String)params.get("skuId"));
            if(skuId != null && skuId > 0) {
                wrapper.eq("sku_id", skuId);
            }
        } catch (Exception e) {
            log.error("skuId转换异常：{}", params.get("skuId"), e);
        }

        try {
            Long wareId = Long.parseLong((String)params.get("wareId"));
            if(wareId != null && wareId > 0) {
                wrapper.eq("ware_id", wareId);
            }
        } catch (Exception e) {
            log.error("wareId转换异常：{}", params.get("wareId"), e);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    // 将采购项加入库存
    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        WareSkuEntity wareSkuEntity = wareSkuDao.selectOne(new QueryWrapper<WareSkuEntity>()
                .eq("sku_id", skuId)
                .eq("ware_id", wareId));
        if(wareSkuEntity == null) {
            WareSkuEntity e = new WareSkuEntity();
            e.setSkuId(skuId);
            e.setWareId(wareId);
            e.setStock(skuNum);
            e.setStockLocked(0);
            // 远程查询sku名字
            try {
                R info = productFeignService.info(skuId);
                if(info.getCode() == 0) {
                    Map<String, Object> skuInfo = (Map<String, Object>)info.get("skuInfo");
                    e.setSkuName((String) skuInfo.get("skuName"));
                }
            } catch (Exception ex) {
                log.error("远程查询skuInfo失败：{}", ex.getMessage(), ex);
            }
            wareSkuDao.insert(e);
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }
    }

    // 查询skuId下的总库存
    @Override
    public int skuStock(Long skuId) {
        Integer skuStock = wareSkuDao.getSkuStock(skuId);
        return skuStock == null ? 0 : skuStock;
    }
}